package com.soft.filter;

import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter {

    private FilterConfig filterConfig = null;
    private String encoding = null;

    @Override
    public void destroy() {
        filterConfig = null;
        encoding = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) throws IOException, ServletException {
        if (request.getCharacterEncoding() == null) {
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
            filterChain.doFilter(request, response);
        }
    }

    @Override//初始化的时候就去web.xml配置文件中拿到对应编码格式
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }
}